<?php

	session_start();
	require("connect.php");
	$link = connect_APO();
	
	require("check.php");
	is_logged_in();
	is_either();
	
	require("header+logoutB.php");
	headerL();
	
	$midweekName=$_GET['midweek_name'];
	
	$numberPeople = $_SESSION['numberPeopleAtMidweek'];
	
	$sql= sprintF("SELECT Credit FROM midweeks WHERE Name='$midweekName'");
	$result=mysql_query($sql);
	$row = mysql_fetch_array($result);
	$amountPending= $row["Credit"];
	
	$sql2 = sprintf("SELECT * FROM `$midweekName`, users WHERE `$midweekName`.ID = users.ID");
	$result2 = mysql_query($sql2);
	
	echo "<html>
<head>
</head>
<body>

<font class='mainFont'>
	<center>
		<div id='midweekTable'>
			<div id='links'>
				<div id='backLink'> <A href='index.php'>Back to Calendar</A></div>";
				if($leader){
					echo "<div id='emailReminder'> <A href='emailReminder.php?midweek_name=$midweekNameOld''>Email Reminder</A></div>";
				}
			
			echo "</div>
			<b class='b1f'></b><b class='b2f'></b><b class='b3f'></b><b class='b4f'></b>
			<div class='contentf'>";
	
	for($i=0; $i<$numberPeople; $i+=1){
		$pending = $_POST["person".$i];
		$row2 = mysql_fetch_array($result2);
		if(strcmp($pending, "Yes")==0){
			$user = $row2["ID"];
			$FName = $row2["Fname"];
			$LName = $row2["Lname"];
			$currentPending = $row2["Pending"];
			$newPending = $amountPending + $currentPending;
			$currentPL = $row2["PendingLocation"];
			if(strcmp($currentPL,"")==0){
				$newPL = $midweekName;
			} else {
				$newPL = $currentPL.", ".$midweekName;
			}
			$sql3 = sprintF("UPDATE `users` SET `Pending` = '$newPending', `PendingLocation` = '$newPL' WHERE `users`.`ID` = '$user'");
			$result3=mysql_query($sql3);
			echo "<div>User $FName $LName now has $amountPending more pending midweek hours.</div>";
		}
	}
	
	$sql3 = sprintF("UPDATE midweeks SET Closed = 0 WHERE Name = '$midweekName'");
	$result3=mysql_query($sql3);
	$sql3 = sprintF("TRUNCATE TABLE `$midweekName`");
	$result3=mysql_query($sql3);
	
	echo "<div><b>The midweek has now been reopened.</b></div>
	</div>
	  <b class='b4f'></b><b class='b3f'></b><b class='b2f'></b><b class='b1f'></b>
		</div>
	</center>
</font>
</body>
</head>
</html>";

	require("footer.php");
	footer();
	mysql_close($link);

?>
